package com.zyf.wjy.controller;

import com.zyf.wjy.common.Result;
import com.zyf.wjy.entity.PlantingPlan;
import com.zyf.wjy.service.CropService;
import com.zyf.wjy.service.FieldService;
import com.zyf.wjy.service.PlantingPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 种植计划控制器
 */
@Controller
@RequestMapping("/plan")
public class PlantingPlanController {
    
    @Autowired
    private PlantingPlanService plantingPlanService;
    
    @Autowired
    private FieldService fieldService;
    
    @Autowired
    private CropService cropService;
    
    /**
     * 种植计划列表页面
     */
    @GetMapping("/list")
    public String list(Model model) {
        List<PlantingPlan> plans = plantingPlanService.findAll();
        model.addAttribute("plans", plans);
        model.addAttribute("fields", fieldService.findAll());
        model.addAttribute("crops", cropService.findAll());
        return "plan/list";
    }
    
    /**
     * 获取计划详情
     */
    @GetMapping("/detail/{id}")
    @ResponseBody
    public Result<PlantingPlan> detail(@PathVariable Long id) {
        PlantingPlan plan = plantingPlanService.findById(id);
        if (plan != null) {
            return Result.success(plan);
        } else {
            return Result.error("计划不存在");
        }
    }
    
    /**
     * 新增/编辑页面
     */
    @GetMapping("/edit")
    public String edit(@RequestParam(required = false) Long id, Model model) {
        if (id != null) {
            PlantingPlan plan = plantingPlanService.findById(id);
            model.addAttribute("plan", plan);
        }
        model.addAttribute("fields", fieldService.findAll());
        model.addAttribute("crops", cropService.findAll());
        return "plan/edit";
    }
    
    /**
     * 保存种植计划
     */
    @PostMapping("/save")
    @ResponseBody
    public Result<PlantingPlan> save(@RequestBody PlantingPlan plan) {
        try {
            PlantingPlan saved = plantingPlanService.save(plan);
            return Result.success("保存成功", saved);
        } catch (Exception e) {
            return Result.error("保存失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除种植计划
     */
    @PostMapping("/delete/{id}")
    @ResponseBody
    public Result<Void> delete(@PathVariable Long id) {
        try {
            boolean success = plantingPlanService.deleteById(id);
            if (success) {
                return Result.success("删除成功", null);
            } else {
                return Result.error("删除失败");
            }
        } catch (Exception e) {
            return Result.error("删除失败：" + e.getMessage());
        }
    }
    
    /**
     * 开始执行计划
     */
    @PostMapping("/start/{id}")
    @ResponseBody
    public Result<Void> start(@PathVariable Long id) {
        try {
            plantingPlanService.startPlan(id);
            return Result.success("计划已开始执行", null);
        } catch (Exception e) {
            return Result.error("操作失败：" + e.getMessage());
        }
    }
    
    /**
     * 完成计划
     */
    @PostMapping("/complete/{id}")
    @ResponseBody
    public Result<Void> complete(@PathVariable Long id) {
        try {
            plantingPlanService.completePlan(id);
            return Result.success("计划已完成", null);
        } catch (Exception e) {
            return Result.error("操作失败：" + e.getMessage());
        }
    }
    
    /**
     * 取消计划
     */
    @PostMapping("/cancel/{id}")
    @ResponseBody
    public Result<Void> cancel(@PathVariable Long id) {
        try {
            plantingPlanService.cancelPlan(id);
            return Result.success("计划已取消", null);
        } catch (Exception e) {
            return Result.error("操作失败：" + e.getMessage());
        }
    }
}

